﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 *  MVC控制器基类。
 * 
 * 最后修改：2011-10-29
 * ************************************************************/
namespace Mozlite.Web.UI.Areas.ControlPanel.Controllers
{
    using System;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;
    using Mozlite.Common;

    /// <summary>
    /// 默认控制器。
    /// </summary>
    public class HomeController : ControlPanelBaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ThreadsView()
        {
            int id = this.Get<int>("cid");
            int? categoryID = null;
            if(id > 0)
                categoryID = id;
            int size;
            var threads = Threads.GetThreads(new ThreadQuery { PageIndex = PageIndex - 1, PageSize = AppConfig.ThreadPageSize, IsDesc = true, OrderBy = "ThreadID", CategoryID = categoryID }, out size);
            ViewBag.Pages = GetPages(size, AppConfig.ThreadPageSize);
            return View(threads);
        }

        [HttpPost]
        public ActionResult ThreadsView(FormCollection form)
        {
            switch (form.GetAction())
            { 
                case HttpAction.Delete:
                    
                    return Json(new { message = "恭喜你，你已经成功删除了文章！" });
            }
            return Json(null);
        }

        public ActionResult ThreadView()
        {
            var thread = Threads.GetThread(this.Get<int>("id"));
            if (thread == null)
                thread = new Thread();
            return View(thread);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ThreadView(Thread model)
        {
            if (ModelState.IsValid)
            {
                if (model.Title == null)
                {
                    SetStatus(GLocal(CreateThreadStatus.TitleNotNull), true);
                    return View(model);
                }
                if (model.Body == null)
                {
                    SetStatus(GLocal(CreateThreadStatus.BodyNotNull), true);
                    return View(model);
                }
                if (model.CategoryID == 0)
                {
                    SetStatus(GLocal(CreateThreadStatus.CategoryIDNotSelected), true);
                    return View(model);
                }
                model.ThreadID = this.Get<int>("id");
                var status = Threads.SaveThread(model);
                if (status == CreateThreadStatus.Updated || status == CreateThreadStatus.Created)
                {
                    return RedirectToAction("ThreadsView");
                }
                else
                {
                    SetStatus(GLocal(status), true);
                }
            }
            return View(model);
        }

        public ActionResult CommonView()
        {
            return View(Application.Current.Configuration);
        }

        [HttpPost]
        public ActionResult CommonView(ApplicationConfiguration model)
        {
            if (ModelState.IsValid)
            {
                model.ApplicationID = Application.Current.ApplicationID;
                if (Applications.SaveConfiguration<ApplicationConfiguration>(model))
                    SetResourceStatus("ConfigurationBase.Save.Success");
                else
                    SetResourceStatus("ConfigurationBase.Save.Error", true);
            }
            return View(Application.Current.Configuration);
        }

        public ActionResult CategoryView()
        {
            var categories = from category in Categories.LoadCategories().Values
                             where category.ParentID == 0
                             select category;
            return View(categories);
        }

        [HttpPost]
        public JsonResult CategoryView(FormCollection form)
        {
            switch (form.GetAction())
            {
                case HttpAction.Delete:
                    {
                        var status = Categories.DeleteCategory(form.Get<int>("did"));
                        if (status == CreateCategoryStatus.Deleted)
                            return Json(new { message = Local(status), status = true });
                    return Json(new { message = Local(status) });
                    }
            }
            return Json(null);
        }

        public ActionResult ThemeView() {
            return View(ThemeConfiguration.Themes.Values);
        }

        public ActionResult NavigatorView()
        {
            string key = RouteData.Get<string>("key");
            if (key == null)
            {
                SetResourceStatus("Themes.NotFound");
                return View();
            }
            ViewBag.Theme = key;
            return View();
        }
    }
}
